part of '../repository.dart';

const _kLearningSessionLearningIntervalKey = 'learning_intervals';

class _LearningSessionDto extends Equatable {
  const _LearningSessionDto._internal({
    required this.learningIntervalDtos,
  });

  final BuiltList<_LearningIntervalDto> learningIntervalDtos;

  @override
  List<Object?> get props {
    return [
      learningIntervalDtos,
    ];
  }

  factory _LearningSessionDto.fromJson(Map<String, dynamic> json) {
    final learningIntervalMaps = List.castFrom(json[_kLearningSessionLearningIntervalKey]);

    final learningIntervalDtos = learningIntervalMaps.map((learningIntervalMap) {
      final learningIntervalJson =
          Map.castFrom<dynamic, dynamic, String, dynamic>(learningIntervalMap);

      return _LearningIntervalDto.fromJson(learningIntervalJson);
    }).toBuiltList();

    return _LearningSessionDto._internal(
      learningIntervalDtos: learningIntervalDtos,
    );
  }

  factory _LearningSessionDto.fromEntity(LearningSession learningSession) {
    final learningIntervalDtos = learningSession.learningIntervals.map((learningInterval) {
      return _LearningIntervalDto.fromLearningInterval(learningInterval);
    }).toBuiltList();

    return _LearningSessionDto._internal(
      learningIntervalDtos: learningIntervalDtos,
    );
  }

  Map<String, dynamic> toJson() {
    final learningIntervalJsons = learningIntervalDtos.map((learningIntervalDto) {
      return learningIntervalDto.toJson();
    }).toList();

    return {
      _kLearningSessionLearningIntervalKey: learningIntervalJsons,
    };
  }

  LearningSession toEntity() {
    final learningIntervals = learningIntervalDtos.map((learningIntervalDto) {
      return learningIntervalDto.toEntity();
    }).toBuiltList();

    return LearningSession(
      learningIntervals: learningIntervals,
    );
  }
}
